using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace JobUsersSync.Features.JobUsersSync
{
    /// <summary>
    /// Этот класс обрабатывает события, возникающие в ходе активации, деактивации, установки, удаления и обновления компонентов.
    /// </summary>
    /// <remarks>
    /// GUID, присоединенный к этому классу, может использоваться при создании пакета и не должен изменяться.
    /// </remarks>
    [Guid("db71c5fe-2779-4763-8af5-7eefa81db3c3")]
    public class JobUsersSyncEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                // delete preview job item
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (
                           job.Name == "JobUsersSync_v1"
                        || job.Name == "JobUsersSync_v2"
                        )
                    {
                        job.Delete();
                    }
                }
                JobUsersSync listLoggerJob =
                    new JobUsersSync("JobUsersSync_v1", site.WebApplication);
                SPDailySchedule schedule = new SPDailySchedule();
                schedule.BeginHour = 1;
                schedule.BeginMinute = 0;
                schedule.BeginSecond = 0;
                schedule.EndHour = 1;
                schedule.EndMinute = 59;
                schedule.EndSecond = 59;

                listLoggerJob.Schedule = schedule;
                listLoggerJob.Update();
            }
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                {
                    if (job.Name == "JobUsersSync_v1"
                        || job.Name == "JobUsersSync_v2")
                    {
                        job.Delete();
                    }
                }
            }
        }
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
